package com.service.Impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dao.SysRoleMapper;
import com.entity.SysMenu;
import com.entity.SysRole;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.SysRoleService;
import com.utils.BaseService;
import com.utils.DataTables;
import com.utils.RequestStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author lanouhn
 */
@Service
public class SysRoleServiceImpl extends BaseService implements SysRoleService{
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Override
    public long getSysRoleRowCount(){
        return sysRoleMapper.getSysRoleRowCount();
    }
    @Override
    public List<SysRole> selectSysRole(){
        return sysRoleMapper.selectSysRole();
    }
    @Override
    public SysRole selectSysRoleByObj(SysRole obj){
        return sysRoleMapper.selectSysRoleByObj(obj);
    }
    @Override
    public SysRole selectSysRoleById(Integer id){
        return sysRoleMapper.selectSysRoleById(id);
    }
    @Override
    public int insertSysRole(SysRole value){
        return sysRoleMapper.insertSysRole(value);
    }
    @Override
    public int insertNonEmptySysRole(SysRole value){
        return sysRoleMapper.insertNonEmptySysRole(value);
    }
    @Override
    public int insertSysRoleByBatch(List<SysRole> value){
        return sysRoleMapper.insertSysRoleByBatch(value);
    }
    @Override
    public int deleteSysRoleById(Integer id){
        return sysRoleMapper.deleteSysRoleById(id);
    }
    @Override
    public int updateSysRoleById(SysRole enti){
        return sysRoleMapper.updateSysRoleById(enti);
    }
    @Override
    public int updateNonEmptySysRoleById(SysRole enti){
        return sysRoleMapper.updateNonEmptySysRoleById(enti);
    }

    @Override
    public DataTables getPageList(DataTables dataTables) {
        PageHelper.startPage((dataTables.getStart() / dataTables.getLength()) + 1, dataTables.getLength()); // 核心分页代码
        PageHelper.orderBy("sort asc");

        //判断需要排序的类是否为空
        if(!StringUtils.isEmpty(dataTables.getColumn())){
            String column = propertyToColumn(SysRoleMapper.class, dataTables.getColumn());
            if(!StringUtils.isEmpty(column)){
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }

        PageInfo<SysRole> pageInfo = new PageInfo<SysRole>(sysRoleMapper.getPageList(dataTables.getSearch(), dataTables.getSubSQL()));
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<SysMenu>());

        return dataTables;
    }

    @Override
    public Map<String, Object> addSysRole(SysRole sysRole) {
        Map<String, Object> map = new HashMap<>();
        Integer id = sysRoleMapper.insertSysRole(sysRole);
        if (id > 0) {
            map.put("status", RequestStatus.SUCCESS);
        }else{
            map.put("status", RequestStatus.FAIL);
        }
        /**
         * 获取新插入数据的ID 放到map集合里面
         */
        map.put("id",sysRole.getId());
        return map;
    }

    @Override
    public Map<String, Object> updateSysRole(SysRole sysRole) {
        Map<String, Object> map = new HashMap<>();
        Integer id = sysRoleMapper.updateNonEmptySysRoleById(sysRole);
        if (id > 0) {
            map.put("status", RequestStatus.SUCCESS);
        }else{
            map.put("status", RequestStatus.FAIL);
        }

        return map;
    }

    @Override
    public Map<String, Object> deleteByPrimaryKeys(List<Integer> idList) {
        Map<String, Object> map = new HashMap<>();
        Integer id = sysRoleMapper.deleteByPrimaryKeys(idList);
        if (id > 0) {
            map.put("status", RequestStatus.SUCCESS);
        }else{
            map.put("status", RequestStatus.FAIL);
        }

        return map;
    }

    public SysRoleMapper getSysRoleMapper() {
        return this.sysRoleMapper;
    }

    public void setSysRoleMapper(SysRoleMapper sysRoleMapper) {
        this.sysRoleMapper = sysRoleMapper;
    }

}